package com.wd.mylibrary_my.view.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.Toast
import com.wd.mylibrary_base.base.BaseActivity
import com.wd.mylibrary_base.utils.MyApp
import com.wd.mylibrary_my.R
import com.wd.mylibrary_my.databinding.ActivityChangeSignatureBinding
import com.wd.mylibrary_my.viewmodel.ChangeSignatureViewModel

class ChangeSignatureActivity : BaseActivity<ChangeSignatureViewModel,ActivityChangeSignatureBinding>(R.layout.activity_change_signature) {
    override fun initView() {
        var userId = MyApp.sp.getInt("userId", 0)
        var sessionId = MyApp.sp.getString("sessionId","").toString()

        dataBinding.edi.hint=this.intent.getStringExtra("signature").toString()



        dataBinding.edi.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {
                if(p0.toString().length>0){
                    dataBinding.yes.visibility=View.VISIBLE
                }else{
                    dataBinding.yes.visibility=View.GONE
                }
            }

        })


        //返回
        dataBinding.back.setOnClickListener {
            finish()
        }

        //修改签名
        dataBinding.yes.setOnClickListener {
            var edi = dataBinding.edi.text.toString()

            viewModel.getChangeSignatureData(userId,sessionId,edi)

            MyApp.sp.edit().putString("signature",edi).commit()

        }
    }

    override fun dataObserve() {
        viewModel.changeSignatureData.observe(this,{
            if(it.status=="0000"){
                Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()
                finish()
            }
        })
    }

}